package com.zhouheng.springbootdesignpattern.archetypalpattern;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 复制Boy（深克隆）
 *
 * @author 周恒
 * @date 20190329 17:16:59
 * @since v1.0
 */
@Data
@AllArgsConstructor
public class Deep implements Cloneable {

    private String name;

    private Integer money;

    private boolean marriage;

    private List<String> interest;

    private Car car;

    @Override
    public Deep clone() {
        Deep deep = null;
        try {
            deep = (Deep) super.clone();
            //interest如果是ArrayList直接可以 boy.interest = (List) this.interest.clone(); ArrayList是实现了clone接口的
            // **但是(List) this.interest.clone()这里要报错，List没有实现clone接口
            //其余很多引用类型及大多数容器都实现了clone()方法的
            deep.interest = new ArrayList<>(interest);
            // car也需要重写clone方法，且需要在父类这里手动调用赋值
            deep.car = (Car) this.car.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return deep;
    }

}
